package com.example.mybatisplus.auto.fill;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.example.mybatisplus.identifier.generator.CustomIdentifierGenerator;
import com.example.mybatisplus.pojo.General;
import com.example.mybatisplus.pojo.One;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;


/**
 * 如果使用自定义的sql且有@param参数，那么请指定@param("et"),而且只会为该et参数进行填充。
 */
@Component
public class CustomMetaObjectHandler implements MetaObjectHandler {

    @Autowired
    CustomIdentifierGenerator customIdentifierGenerator;
    @Override
    public void insertFill(MetaObject metaObject) {
        Object originalObject = metaObject.getOriginalObject();
        if(originalObject instanceof One){
            System.out.println(this.getFieldValByName("id", metaObject));
            System.out.println(this.getFieldValByName("oneName", metaObject));
            //进行填充
            this.setFieldValByName("oneCreateTime",new Date(),metaObject);
        }else if(originalObject instanceof General){
            this.setFieldValByName("version",1L,metaObject);
            this.setFieldValByName("logicDeleteState",false,metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
    }
}
